﻿


Imports System.Data.OleDb
Imports System.IO

Public Class Form1
    ' Public title As String = ""
    Dim dcol() As Integer = {(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0)}
    Dim flag As Boolean = False
    Dim isOpen As Boolean = False
    Public action As String = ""
    Public Did As Integer
    Dim html As String



    Public Sub actions()
        flag = False
        TabControl1.Visible = True
        ExportToolStripMenuItem.Enabled = True


        Dim strSQL2 As String = "SELECT * FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text & " OR TemplateId =0"

        Dim layernumber As Integer

        Dim content As String = ""

        Dim text As HtmlDocument = WebBrowser2.Document
        html = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN''http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
        html += "<html xmlns='http://www.w3.org/1999/xhtml'><head><meta content='text/html; charset=utf-8' http-equiv='Content-Type' /><title id='toptitle'></title></head><body >"
        html += "<div style='position: absolute; width: 100%; height: 100%; z-index: 1' id='Main'><h1><center><div style=' position: absolute; top:0px; left:40%; width: 300px; height: 20%;  overflow-x: hidden;	word-wrap: break-word ; z-index: 1' id='9'></div></center></h1><div style=' position: absolute; width: 100%; top:25%; height:75%; z-index: 1; left: 0px;' id='Core'>"

        Try
            Using adapter2 As New OleDbDataAdapter(strSQL2, My.Settings.DigitalDocumentConnectionString)


                Dim ds2 As New DataSet
                adapter2.Fill(ds2, "Layer")


                Me.LayerComboBox.DataSource = ds2.Tables("Layer")
                Me.LayerComboBox.ValueMember = "Lid"
                Me.LayerComboBox.DisplayMember = "Type"


            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


        Dim strSQL As String = "SELECT count(*) as cc FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text
        Dim strSQL1 As String = "SELECT *  FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text

        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            layernumber = dr("cc")
                        End While
                        cnn.Close()

                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            html = "Problem"
        End Try


        If action = "New" Then
            MsgBox("New")
            isOpen = False


            Try
                Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                    Dim layer(0 To layernumber) As String
                    Dim counter As Integer = 0

                    Using cmd As New OleDbCommand(strSQL1, cnn)
                        cnn.Open()

                        Using dr As OleDbDataReader = cmd.ExecuteReader()

                            Me.ContentTableAdapter1.Insert(Me.Did, 9, "", "")
                            While dr.Read
                                layer(counter) = "<div id='" + CStr(dr("Lid")) + "' style='background-color:gray; position:" + dr("Position") + "; top:" + dr("PossitionY") + "; left:" + dr("PossitionX") + "; width:" + dr("DissmentionX") + ";  " + dr("InnerLayer") + "'></div>" '<input type='hidden' id='flag' value='0'>
                                Me.ContentTableAdapter1.Insert(Me.Did, dr("Lid"), "", "")
                                counter = counter + 1
                            End While
                        End Using
                    End Using

                    Dim intCount As Integer

                    For intCount = 0 To layernumber
                        content += layer(intCount)

                    Next intCount



                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                html = "Problem"
            End Try



        ElseIf action = "Open" Then
            isOpen = True

            Try
                Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                    Dim layer(0 To layernumber) As String
                    Dim counter As Integer = 0

                    Using cmd As New OleDbCommand(strSQL1, cnn)
                        cnn.Open()

                        Using dr As OleDbDataReader = cmd.ExecuteReader()


                            While dr.Read
                                layer(counter) = "<div id='" + CStr(dr("Lid")) + "' style='background-color:gray; position:" + dr("Position") + "; top:" + dr("PossitionY") + "; left:" + dr("PossitionX") + "; width:" + dr("DissmentionX") + "; " + dr("InnerLayer") + "' ></div>"
                                counter = counter + 1
                            End While
                        End Using
                    End Using

                    Dim intCount As Integer

                    For intCount = 0 To layernumber
                        content += layer(intCount)

                    Next intCount



                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                html = "Problem"
            End Try
        End If


        html += content
        html += "</div></div></body></html>"


        WebBrowser2.DocumentText = html



        flag = True


    End Sub
    Private Sub NewToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem1.Click
        Form2.Show()
    End Sub



    Private Sub ImportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportToolStripMenuItem.Click
        OpenDocument.Show()
    End Sub



    'optional
    Private Sub HiddenTemp_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)




    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TabControl1.Visible = False
        ExportToolStripMenuItem.Enabled = False
        

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Context.Text = Context.Text & "<h1></h1>"

    End Sub

    Private Sub h2B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles h2B.Click
        Context.Text = Context.Text & "<h2></h2>"
    End Sub

    Private Sub h3B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles h3B.Click
        Context.Text = Context.Text & "<h3></h3>"
    End Sub

    Private Sub h4B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles h4B.Click
        Context.Text = Context.Text & "<h4></h4>"
    End Sub

    Private Sub CenterB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles paragrah.Click
        Context.Text = Context.Text & "<p></p>"

    End Sub

    Private Sub brB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brB.Click
        'Context.Text = Context.Text & "<br />"
        MsgBox(WebBrowser2.DocumentText.Length)
    End Sub

    Private Sub UB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UB.Click
        Context.Text = Context.Text & "<u></u>"
    End Sub

    Private Sub BB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BB.Click
        Context.Text = Context.Text & "<strong></strong>"
    End Sub

    Private Sub ExportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToolStripMenuItem.Click


        SaveFileDialog1.Filter = "Html Files (*.html) |*.html"
      

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim objWriter As New System.IO.StreamWriter(SaveFileDialog1.FileName, False)
            display()
            objWriter.WriteLine(html)
            objWriter.Close()
        End If


    End Sub

    Private Sub TestToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        main.Show()

    End Sub

    Private Sub WebBrowser2_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted

        If isOpen Then
            Dim text As HtmlDocument = WebBrowser2.Document

            Dim sql As String = "SELECT * FROM Content WHERE Did =" + Did.ToString
            Try
                Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                    Using cmd As New OleDbCommand(sql, cnn)
                        cnn.Open()

                        Using dr As OleDbDataReader = cmd.ExecuteReader()


                            While dr.Read
                                text.GetElementById(dr("Lid")).SetAttribute("innerHTML", dr("InnerHtml1") + "" + dr("InnerHtml2"))


                            End While
                        End Using
                    End Using





                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)

            End Try

            Context.Text = text.GetElementById(LayerComboBox.SelectedValue).GetAttribute("innerHTML")


        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (LayerComboBox.Text = "") Then
            MsgBox("Διάλεξαι Layer")
        Else
            If (Context.Text = "") Then
                MsgBox("Συμπλήρωσε το πεδίο")
            Else
                Dim x As Integer = 0
                Dim idl As String
                Dim layer As Integer
                Dim text As HtmlDocument = WebBrowser2.Document
                Dim content As String
                Dim layertitle As String
                layer = LayerComboBox.SelectedValue

                content = text.GetElementById(layer).GetAttribute("innerHTML")


                dcol(LayerComboBox.SelectedIndex) = dcol(LayerComboBox.SelectedIndex) + 1

                'text.GetElementById(layer).SetAttribute("innerHTML", content + "<div id='col" + CStr(dcol(LayerComboBox.SelectedIndex)) + "'>" + Context.Text + "</div>")


                text.GetElementById(layer).SetAttribute("innerHTML", Context.Text)




                content = text.GetElementById(layer).GetAttribute("innerHTML")


                Dim w As String = ""
                Dim y As String = ""
                If (content.Length > 254) Then
                    w = content.Substring(0, 254)
                    y = content.Substring(254)

                    '  MsgBox(w)
                    ' MsgBox(y)
                Else
                    w = content

                End If


                '  Dim strSQL As String = "UPDATE Content SET InnerHtml1='" + w.ToString + "', InnerHtm2='" + y.ToString + "' WHERE Did=" + Did.ToString + " AND Lid=" + layer.ToString
                Dim sql As String = "SELECT * FROM Content WHERE Did =" + Did.ToString + " AND Lid =" + layer.ToString


                Try
                    Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)



                        Using cmd As New OleDbCommand(sql, cnn)
                            cnn.Open()

                            Using dr As OleDbDataReader = cmd.ExecuteReader()


                                While dr.Read
                                    Me.ContentTableAdapter1.Update(Did, layer, w, y, dr("Cid"), Did, layer, dr("InnerHtml1"), dr("InnerHtml2"))
                                End While
                            End Using
                        End Using





                    End Using
                Catch ex As Exception
                    MessageBox.Show(ex.Message)

                End Try




            End If
        End If

    End Sub

    Private Sub LayerComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LayerComboBox.SelectedIndexChanged
        'MsgBox(LayerComboBox.Text + " and " + LayerComboBox.SelectedValue.ToString)

        Dim text As HtmlDocument = WebBrowser2.Document

        If flag Then

            Context.Text = text.GetElementById(LayerComboBox.SelectedValue).GetAttribute("innerHTML")



        End If


        '  

    End Sub




    Private Sub a(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    End Sub

    Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        display()

        WebBrowser3.DocumentText = html

    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        DeleteDoc.Show()

    End Sub

    Private Sub Form1TempText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1TempText.TextChanged
        Dim strSQL1 As String = "SELECT Title FROM Document WHERE DId =" & Did.ToString


        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)
                Using cmd As New OleDbCommand(strSQL1, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            Form1DocText.Text = dr("Title")
                        End While
                        cnn.Close()

                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub


    Private Sub display()



        Dim strSQL2 As String = "SELECT * FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text & " OR TemplateId =0"

        Dim layernumber As Integer

        Dim content As String = ""

        Dim text As HtmlDocument = WebBrowser2.Document
        html = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN''http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
        html += "<html xmlns='http://www.w3.org/1999/xhtml'><head><meta content='text/html; charset=utf-8' http-equiv='Content-Type' /><title id='toptitle'></title></head><body >"
        html += "<div style='position: absolute; width: 100%; height: 100%; z-index: 1' id='Main'><h1><center><div style=' position: absolute; top:0px; left:40%; width: 300px; height: 20%;  overflow-x: hidden;	word-wrap: break-word ; z-index: 1' id='9'>"
        Dim sql As String = "SELECT * FROM Content WHERE Did =" + Did.ToString + " AND Lid =9"


        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)



                Using cmd As New OleDbCommand(sql, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            html += dr("InnerHtml1") + "" + dr("InnerHtml2")
                        End While
                    End Using
                End Using





            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try



        html += "</div></center></h1><div style=' position: absolute; width: 100%; top:25%; height:75%; z-index: 1; left: 0px;' id='Core'>"



        Dim strSQL As String = "SELECT count(*) as cc FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text
        Dim strSQL1 As String = "SELECT  Layer.*,Content.InnerHtml1,Content.InnerHtml2 FROM (Document INNER JOIN Content ON Document.DId = Content.Did) INNER JOIN Layer ON Content.Lid = Layer.Lid WHERE Content.Did=" + Did.ToString

        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            layernumber = dr("cc")
                        End While
                        cnn.Close()

                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            html = "Problem"
        End Try


        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                Dim layer(0 To layernumber) As String
                Dim counter As Integer = 0

                Using cmd As New OleDbCommand(strSQL1, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read

                            layer(counter) = "<div id='" + CStr(dr("Lid")) + "' style='background-color:gray; position:" + dr("Position") + "; top:" + dr("PossitionY") + "; left:" + dr("PossitionX") + "; width:" + dr("DissmentionX") + ";  " + dr("InnerLayer") + "'>" + dr("InnerHtml1") + "" + dr("InnerHtml2") + "</div>" '<input type='hidden' id='flag' value='0'>

                            counter = counter + 1
                        End While
                    End Using
                End Using

                Dim intCount As Integer

                For intCount = 0 To layernumber
                    content += layer(intCount)

                Next intCount



            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            html = "Problem"
        End Try





        html += content
        html += "</div></div></body></html>"





    End Sub




    Private Sub NewToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem4.Click
        Form5.Show()

    End Sub

    Private Sub ChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeToolStripMenuItem.Click
        Form3.Show()
    End Sub

    Private Sub DeleteToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem3.Click
        Form4.Show()
    End Sub

    Private Sub NewToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem2.Click
        NewTemplate.Show()
    End Sub

    Private Sub DeleteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem1.Click
        DeleteTemplate.Show()
    End Sub
End Class
